<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <p style="color: red;">局部组件：</p>
        <my-button></my-button>
        <my-button></my-button>
        <my-button></my-button>
        <my-button></my-button>
        
        <p style="color: red;">全局组件：</p>
        <global-component></global-component><br>
        <global-button></global-button>

        <p style="color: red;">综合组件：</p>
        <my-panel></my-panel>
    </div>

    <template id="button">
        <button @click="count++" class="btn">按钮点击次数：{{count}}</button>
    </template>

    <template id="method-test">
        <!-- 只能有一个最顶级的根元素，因此需要使用div包裹 -->
        <div>
            <button @click="sub">-1</button>
            <span>{{ count }}</span>
            <button @click="add">+1</button> 
        </div>
    </template>

    <template id="panel">
        <div>
            <div class="header">
                <span class="title">自由与爱情</span>
                <span @click="isShow = !isShow" style="cursor: pointer;">{{ isShow ? "折叠" : "展开" }}</span>
            </div>
            <div class="content" v-if="isShow">
                <ul>
                    <li>生命诚可贵,</li>
                    <li>爱情价更高。</li>
                    <li>若为自由故,</li>
                    <li>两者皆可抛。</li>
                </ul>
            </div>
        </div>
    </template>

    <script>
        // == 定义全局组件 ==
        Vue.component(
            "my-panel", {
                template: "#panel",
                data() {
                    return {
                        isShow: true
                    }
                }
            }
        )

        Vue.component(
            "global-component", {
                // 若是组件样式简单，可以写成一行，与事件绑定函数简写一样
                template: '<p>{{ msg }}</p>',
                data() {
                    return {
                        msg: "Hello World!"
                    }
                }
            }
        )

        Vue.component(
            "global-button", {
                template: '#method-test',
                data() {
                    return {
                        count: 0,
                    }
                },
                // 组件中，方法的定义形式与正常一致
                methods: {
                    add() {
                        this.count ++
                    },
                    sub() {
                        this.count --
                    }
                }
            }
        )


        new Vue({
            el: "#app",
            data: {

            },
            // == 定义局部组件 ==
            components: {
                // 组件名称只能小写，大写无法识别
                "my-button": {
                    // 绑定组件样式
                    template: "#button",
                    // 定义组件数据（需要在方法中定义，并返回数据对象）
                    data() {
                        return {
                            count: 0,
                        }
                    }
                }
            }
        })
    </script>
    <style>
        .btn{
            background-color: skyblue;
            color: white;
            border: none;
            border-radius: 2px;
            height: 35px;
            cursor: pointer;
        }
        .header {
            box-sizing: border-box;
            width: 250px;
            border: 1px solid #ccc;
            padding: 0 8px;
            display: flex;
            justify-content: space-between;
        }
        .title {
            font-weight: bolder;
        }
        .content {
            box-sizing: border-box;
            width: 250px;
            border: 1px solid #ccc;
            padding: 4px 0;
        }
        .content ul{
            list-style: none;
            margin: 0;
            padding: 0;
        }
        .content li{
            padding: 4px 8px;
        }

    </style>
</body>
</html>